<?php


class menu extends main {

    function __construct(){
            parent::__construct();
            $this->loadModel('g5_admin_menu');
            $this->loadModel('g5_admin_menu_group');
            $this->loadModel('g5_admin_group');
    }

    function index(){
        $menu=$this->g5_admin_menu->by('menu_order', 'asc')->getAll('*');
        $this->tree =    new Tree($menu);
        $this->tree->set_array_colum(array("id"=>"menu_id","parent_id"=>"top_id","name"=>"menu_name"));
        $menu=$this->tree->get_tree(0,"\$spacer\$menu_name");
        $this->menu         = $menu;
        $this->mainurl      = BASEURI."menu/index";
        $this->formAction   = BASEURI."menu/delete";
        $this->is_validator = true;
        $this->adminlog($this->systemLang['searchMenuList']);
    }

    function add(){
        if($_POST){
            $menu  = $_POST['menu'];
            $groups = $_POST['groups'];
            if($menu_id = $this->g5_admin_menu->insert($menu)){
                foreach($groups as $group_id){
                    $this->g5_admin_menu_group->replace(array('menu_id'=>$menu_id, 'group_id'=>$group_id));
                }
                $this->g5_admin_menu->makecache();
                $this->adminlog($this->systemLang['addMenuSuccess'].$insert);
                $this->report($this->systemLang['addMenuSuccess'], BASEURI.'menu/index');
            }else{
                $this->report($this->systemLang['addMenuFailed'], BASEURI.'menu/index');
            }
        }else{
            $menus=$this->g5_admin_menu->by('menu_order')->getAll('*');
            $group = $this->g5_admin_group->getGroupList();
            $this->tree =    new Tree($menus);
            $this->tree->set_array_colum(array("id"=>"menu_id","parent_id"=>"top_id","name"=>"menu_name"));
            $menu=$this->tree->get_tree(0,"\$spacer\$menu_name");
            $this->formAction = BASEURI."menu/add";
            $this->is_validator = TRUE;
            $this->group = $group;
            $this->menus = $menu;
        }
    }

    function edit(){
        $id = isset($_GET[2]) ? $_GET[2] : 0;
        if($_POST){
            $menu     = $_POST['menu'];
            $groups    = $_POST['groups'];
			$up_menu= $this->g5_admin_menu->update($menu, array('menu_id'=>$_POST['menu_id']));
			$groupid_number = $this->g5_admin_menu_group->getCount('*',array('menu_id'=>$_POST['menu_id']));
			$post_groupid_number = count($groups);
			$group_flag = $post_groupid_number != $groupid_number ? 1 : 0;
            if($up_menu || $group_flag){
                $this->g5_admin_menu_group->delete(array('menu_id'=>$_POST['menu_id']));
                foreach($groups as $group_id){
                    $this->g5_admin_menu_group->replace(array('menu_id'=>$_POST['menu_id'], 'group_id'=>$group_id));
                }
                $this->g5_admin_menu->makecache(); // 更新菜单缓存
                $this->adminlog($this->systemLang['editMenuSuccess'].$edit['id']);
                $this->report($this->systemLang['editMenuSuccess'], BASEURI.'menu/index');
            }else{
                  $this->report($this->systemLang['editMenuFailed'], BASEURI.'menu/index');
            }
        }else{
            $group = $this->g5_admin_group->getGroupList();
            $menus = $this->g5_admin_menu->by('menu_order')->getAll('*');
            $this->tree =    new Tree($menus);
            $this->tree->set_array_colum(array("id"=>"menu_id","parent_id"=>"top_id","name"=>"menu_name"));
            $menus        = $this->tree->get_tree(0,"\$spacer\$menu_name");
            $menu = $this->g5_admin_menu->getOne('*', array('menu_id'=>$id));
            $menu_roles    = $this->g5_admin_menu_group->getroles($id);
            $this->formAction   = BASEURI."menu/edit";
            $this->menus        = $menus;
            $this->is_validator = "TRUE";
            $this->group        = $group;
            $this->menu_roles   = $menu_roles;
            $this->menu  = $menu;
            $this->display('menu.add');
        }
    }

    function delete(){
        $ids = isset($_GET[2]) ? array($_GET[2]) : $_POST['select_id'];
        if($this->g5_admin_menu->in(array('menu_id'=>$ids))->delete()){
            $this->g5_admin_menu_group->in(array('menu_id'=>$ids))->delete();
            $this->g5_admin_menu->makecache(); //更新菜单缓存
            $this->adminlog($this->systemLang['delMenuSuccess'].implode(',', $ids));
            $this->report($this->systemLang['delMenuSuccess'], BASEURI.'menu/index');
        }else{
            $this->report($this->systemLang['delMenuFailed'], BASEURI.'menu/index');
        }
    }

    function assignMenu(){
        $group_id = isset($_GET[2]) ? $_GET[2] : 0;
        if(  ! empty($_POST) ){
            $group_id    =    $_POST['group_id'];
            $menus        =    $_POST['menu_id'];
            if($this->g5_admin_menu_group->assignMenuGroup($menus,$group_id)){
                $this->adminlog($this->systemLang['assignMenuSuceess'].$group_id);
                $this->g5_admin_menu->makecache(); // 更新菜单缓存
                $this->report($this->systemLang['assignMenuSuceess'], BASEURI.'menu/assignmenu/'. $group_id);
            }else{
                $this->report($this->systemLang['assignMenuFailed'], BASEURI.'menu/assignmenu/'. $group_id);
            }
        }

        $group_menu = array();
        $group_menu = $this->g5_admin_menu_group->getAll('*', array('group_id'=>$group_id));
        $mymenus = array();
        foreach($group_menu as $row){
            $mymenus[] = $row['menu_id'];
        }
        $menu   = $this->g5_admin_menu->by('menu_order', 'asc')->getAll('*', array('top_id'=>0));
        foreach($menu as $row){
                $leve_1[$row['menu_id']]    = $this->g5_admin_menu->getAll('*', array('top_id'=>$row['menu_id']));
                foreach ($leve_1[$row['menu_id']] as $m){
                    $leve_1[$m['menu_id']]  = $this->g5_admin_menu->getAll('*', array('top_id'=>$m['menu_id']));
                }
        }
        $this->formAction = BASEURI."menu/assignmenu";
        $this->menuII     = $leve_1;
        $this->menu       = $menu;
        $this->group_id    = $group_id;
        $this->mymenus      = $mymenus;
    }

    function order(){
        if( ! empty($_POST) ){
            $menuId    =    $_POST['menuid'];
            $orders    =    $_POST['orders'];
            $i=0;
            foreach($orders as $order){
                $this->g5_admin_menu->update(array('menu_order'=>$order), array('menu_id'=>$menuId[$i++]));
            }
            //更新菜单缓存
            $this->g5_admin_menu->makecache();
            $this->report($this->systemLang['operaterSuccess'], BASEURI.'menu/index');
        }
    }

}

